<template>
  <Transition name="toc-slide" mode="out-in">
    <div 
      v-if="tocVisible && !tocCollapsed"
      class="toc-container"
      :class="{ 'mobile': isMobile }"
    >
      <div class="toc-header">
        <div class="toc-title">
          <i class="fas fa-list-ul"></i>
          <h3>目录</h3>
        </div>
        <button 
          class="toc-toggle-btn" 
          @click="$emit('toggle')"
          :title="isMobile ? '收起目录' : '收起目录'"
        >
          <i class="fas fa-chevron-left"></i>
        </button>
      </div>
      
      <div class="toc-content">
        <nav class="toc-nav" v-if="headings.length > 0">
          <ul class="toc-list">
                         <li 
               v-for="heading in headings" 
               :key="heading.id"
               class="toc-item"
               :class="[
                 `level-${heading.level}`,
                 { 'active': activeHeading === heading.id }
               ]"
             >
              <a 
                :href="`#${heading.id}`"
                class="toc-link"
                :title="heading.text"
                @click.prevent="scrollToHeading(heading.id)"
              >
                <span class="toc-text">{{ heading.text }}</span>
                <span v-if="activeHeading === heading.id" class="active-indicator"></span>
              </a>
            </li>
          </ul>
        </nav>
        
        <div v-else class="toc-empty">
          <i class="fas fa-file-alt"></i>
          <p>暂无目录</p>
        </div>
      </div>
      
      <div class="toc-footer">
        <div class="progress-indicator">
          <div class="progress-text">阅读进度</div>
          <div class="progress-bar">
            <div 
              class="progress-fill" 
              :style="{ width: `${readingProgress}%` }"
            ></div>
          </div>
          <div class="progress-percent">{{ Math.round(readingProgress) }}%</div>
        </div>
      </div>
    </div>
  </Transition>
  
  <!-- 浮动按钮 -->
  <Transition name="float-button">
    <button 
      v-if="tocVisible && tocCollapsed"
      class="toc-float-btn"
      @click="$emit('toggle')"
      :title="'展开目录'"
    >
      <i class="fas fa-list"></i>
      <span class="float-text">目录</span>
    </button>
  </Transition>
</template>

<script setup>
import { computed, ref, onMounted, onUnmounted } from 'vue'

const props = defineProps({
  headings: {
    type: Array,
    default: () => []
  },
  activeHeading: {
    type: String,
    default: ''
  },
  tocCollapsed: {
    type: Boolean,
    default: false
  },
  tocVisible: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['toggle', 'scroll-to'])

const readingProgress = ref(0)
const isMobile = ref(false)

// 滚动到指定标题
const scrollToHeading = (id) => {
  emit('scroll-to', id)
}

// 计算阅读进度
const updateReadingProgress = () => {
  const scrollTop = window.scrollY
  const docHeight = document.documentElement.scrollHeight - window.innerHeight
  const progress = docHeight > 0 ? (scrollTop / docHeight) * 100 : 0
  readingProgress.value = Math.min(100, Math.max(0, progress))
}

// 检查是否为移动设备
const checkMobile = () => {
  isMobile.value = window.innerWidth < 768
}

// 防抖的滚动处理
let scrollTimer = null
const handleScroll = () => {
  if (scrollTimer) clearTimeout(scrollTimer)
  scrollTimer = setTimeout(updateReadingProgress, 16)
}

// 防抖的窗口大小改变处理
let resizeTimer = null
const handleResize = () => {
  if (resizeTimer) clearTimeout(resizeTimer)
  resizeTimer = setTimeout(checkMobile, 100)
}

onMounted(() => {
  checkMobile()
  updateReadingProgress()
  window.addEventListener('scroll', handleScroll, { passive: true })
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
  window.removeEventListener('resize', handleResize)
  if (scrollTimer) clearTimeout(scrollTimer)
  if (resizeTimer) clearTimeout(resizeTimer)
})
</script>

<style scoped>
/* 容器样式 */
.toc-container {
  position: fixed;
  top: 90px;
  left: var(--space-lg);
  width: 300px;
  max-height: calc(100vh - 140px);
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-sticky);
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(10px);
  transition: all var(--transition-normal);
}

.toc-container.mobile {
  position: fixed;
  top: auto;
  bottom: var(--space-lg);
  left: var(--space-md);
  right: var(--space-md);
  width: auto;
  max-height: 60vh;
}

/* 标题栏 */
.toc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-lg);
  border-bottom: 1px solid var(--border-light);
  background: linear-gradient(135deg, var(--color-primary), var(--color-secondary));
  color: var(--text-inverse);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
}

.toc-title {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.toc-title h3 {
  margin: 0;
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
}

.toc-title i {
  font-size: var(--font-size-md);
  opacity: 0.9;
}

.toc-toggle-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: var(--text-inverse);
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
  backdrop-filter: blur(5px);
}

.toc-toggle-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

/* 内容区域 */
.toc-content {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md) 0;
}

.toc-nav {
  padding: 0 var(--space-md);
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-item {
  margin: 0;
  position: relative;
}

.toc-item.level-2 {
  padding-left: 0;
}

.toc-item.level-3 {
  padding-left: var(--space-lg);
}

.toc-item.level-4 {
  padding-left: var(--space-2xl);
}

.toc-item.level-5,
.toc-item.level-6 {
  padding-left: var(--space-3xl);
}

.toc-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-decoration: none;
  color: var(--text-secondary);
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
  font-size: var(--font-size-sm);
  line-height: var(--line-height-normal);
  position: relative;
  border-left: 3px solid transparent;
}

.toc-link:hover {
  background: var(--color-primary-light);
  color: var(--color-primary);
  border-left-color: var(--color-primary);
  transform: translateX(2px);
}

.toc-item.active .toc-link {
  background: var(--color-primary-light);
  color: var(--color-primary);
  font-weight: var(--font-weight-medium);
  border-left-color: var(--color-primary);
}

.toc-text {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.active-indicator {
  width: 6px;
  height: 6px;
  background: var(--color-primary);
  border-radius: var(--radius-full);
  flex-shrink: 0;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* 空状态 */
.toc-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-2xl);
  color: var(--text-tertiary);
  text-align: center;
}

.toc-empty i {
  font-size: var(--font-size-2xl);
  margin-bottom: var(--space-md);
  opacity: 0.5;
}

.toc-empty p {
  margin: 0;
  font-size: var(--font-size-sm);
}

/* 底部进度指示器 */
.toc-footer {
  padding: var(--space-lg);
  border-top: 1px solid var(--border-light);
  background: var(--bg-secondary);
  border-radius: 0 0 var(--radius-xl) var(--radius-xl);
}

.progress-indicator {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.progress-text {
  font-size: var(--font-size-xs);
  color: var(--text-secondary);
  font-weight: var(--font-weight-medium);
  text-align: center;
}

.progress-bar {
  height: 4px;
  background: var(--border-light);
  border-radius: var(--radius-full);
  overflow: hidden;
  position: relative;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--color-primary), var(--color-secondary));
  border-radius: var(--radius-full);
  transition: width var(--transition-fast);
  position: relative;
}

.progress-fill::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 20px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3));
  animation: shimmer 2s infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-20px); }
  100% { transform: translateX(20px); }
}

.progress-percent {
  font-size: var(--font-size-xs);
  color: var(--text-tertiary);
  text-align: center;
  font-weight: var(--font-weight-medium);
}

/* 浮动按钮 */
.toc-float-btn {
  position: fixed;
  top: 120px;
  left: var(--space-lg);
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, var(--color-accent), var(--color-accent-hover));
  color: var(--text-inverse);
  border: none;
  border-radius: var(--radius-full);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  box-shadow: var(--shadow-lg);
  transition: all var(--transition-normal);
  z-index: var(--z-sticky);
  backdrop-filter: blur(10px);
  border: 2px solid rgba(255, 255, 255, 0.2);
}

.toc-float-btn:hover {
  transform: scale(1.1) rotate(5deg);
  box-shadow: var(--shadow-xl);
}

.toc-float-btn:active {
  transform: scale(0.95);
}

.toc-float-btn i {
  font-size: var(--font-size-lg);
}

.float-text {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

/* 动画 */
.toc-slide-enter-active,
.toc-slide-leave-active {
  transition: all var(--transition-normal);
}

.toc-slide-enter-from {
  transform: translateX(-100%);
  opacity: 0;
}

.toc-slide-leave-to {
  transform: translateX(-100%);
  opacity: 0;
}

.float-button-enter-active,
.float-button-leave-active {
  transition: all var(--transition-normal);
}

.float-button-enter-from {
  transform: scale(0) rotate(180deg);
  opacity: 0;
}

.float-button-leave-to {
  transform: scale(0) rotate(-180deg);
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .toc-container {
    left: var(--space-md);
    width: 280px;
  }
  
  .toc-float-btn {
    left: var(--space-md);
    width: 50px;
    height: 50px;
  }
}

@media (max-width: 768px) {
  .toc-container {
    position: fixed;
    top: auto;
    bottom: var(--space-md);
    left: var(--space-sm);
    right: var(--space-sm);
    width: auto;
    max-height: 50vh;
  }
  
  .toc-float-btn {
    bottom: 100px;
    top: auto;
    left: 50%;
    transform: translateX(-50%);
    width: 50px;
    height: 50px;
  }
  
  .toc-float-btn:hover {
    transform: translateX(-50%) scale(1.1);
  }
}
</style> 